package calvin.study.rabbitmq.service.impl;

import java.util.Map;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import calvin.study.rabbitmq.service.IConsumeService;

/**
 * 短信消费服务
 * @author wh
 *
 */
//bindings其实就是用来确定队列和交换机绑定关系
@RabbitListener(bindings =@QueueBinding(
     // email.fanout.queue 是队列名字，这个名字你可以自定随便定义。
     value = @Queue(value = "sms.fanout.queue",autoDelete = "false"),
     // order.fanout 交换机的名字 必须和生产者保持一致
     exchange = @Exchange(value = "fanout_order_exchange",
             // 这里是确定的rabbitmq模式是：fanout 是以广播模式 、 发布订阅模式
             type = ExchangeTypes.FANOUT)
))
@Component
public class SmsService implements IConsumeService {
	@RabbitHandler
	public void sendMsg(String message,@Headers Map<String,Object> headers, Channel channel) throws Exception {
		// 此处省略发邮件的逻辑
        System.out.println("sms-------------->" + message);
        
        Long deliveryTag  = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);

        boolean multiple = false;

        //手工ACK,确认一条消息已经被消费
        channel.basicAck(deliveryTag,multiple);
	}
}
